package com.ruoyi.web.utils;

import com.alibaba.fastjson2.JSON;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;

@Slf4j
public class SmsUtil {

    private static final String ACCESS_KEY_ID = "LTAI5tJYpL8EZ6Z5YsWiq7Af";//AccessKey自己账号的
    private static final String ACCESS_KEY_SECRET = "RIfCvi5FF7muEeQ348iKx9hMRHwvJ2";
    private static final String ENDPOINT = "dysmsapi.aliyuncs.com";//固定死
    private static final String SIGNNAME = "臻享出行";//签名名称
    private static final String TCODE = "SMS_311295251";//模版的编号
    private static final String TCODE2 = "SMS_478425370";//模版的编号

    public static String  sendSms(String phoneNumbers) throws Exception {

        String code=generateVerificationCode();

        Config config = new Config()
                .setAccessKeyId(ACCESS_KEY_ID)
                .setAccessKeySecret(ACCESS_KEY_SECRET)
                .setEndpoint(ENDPOINT);

        Client client = new Client(config);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phoneNumbers)
                .setSignName(SIGNNAME)
                .setTemplateCode(TCODE)
                //此处是设计模版的时候预留的变量${code}就验证码，用下面的随机生成4位数字传入
                .setTemplateParam(String.format("{\"code\":\"%s\"}", code));

        try {
            SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            log.info("++++++++++++++++++++++++++++++++短信发送结果++++++++++++++++++++++++++++++++");
            log.info("++++++++++++++++++++++++++++++++"+ JSON.toJSONString(sendSmsResponse)+"++++++++++++++++++++++++++++++++");
            //短信验证码失败返回6666
           if(!sendSmsResponse.body.code.equals("OK")){
               return "6666";
           }
            return code;
        } catch (TeaException error) {
            return "6666";
        }
    }


    //卡卷不足发送提醒短信
    public static void sendRemind() throws Exception {

        Config config = new Config()
                .setAccessKeyId(ACCESS_KEY_ID)
                .setAccessKeySecret(ACCESS_KEY_SECRET)
                .setEndpoint(ENDPOINT);

        Client client = new Client(config);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers("18856697728")
                .setSignName(SIGNNAME)
                .setTemplateCode(TCODE2);


        try {
            SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            log.info("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            log.info("推送短信数量不足===>{}",sendSmsResponse);
        } catch (TeaException error) {
        }
    }

    /**
     * 验证码随机四位
     */
    private static String generateVerificationCode() {
        // 生成四位数字验证码
        Random random = new Random();
        int code = random.nextInt(10000); // 随机生成 0 到 9999 的整数
        return String.format("%04d", code); // 保证生成的是四位数字，不足四位时前面补0
    }
}

